package com.vr.cloudpicture.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import com.vr.cloudpicture.common.BaseResponse;
import com.vr.cloudpicture.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 *
 * @author hzh
 * @date 2025/04/24
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 拦截so-token未登录异常，转换自己的异常
     *
     * @param e 异常
     * @return {@code BaseResponse<?> }
     */
    @ExceptionHandler(NotLoginException.class)
    public BaseResponse<?> handlerNotLoginException(NotLoginException e) {
        log.error("NotLoginException", e);
        return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR, e.getMessage());
    }

    /**
     * 拦截so-token没有权限，转换自己的异常
     *
     * @param e 异常
     * @return {@code BaseResponse<?> }
     */
    @ExceptionHandler(NotPermissionException.class)
    public BaseResponse<?> handlerNotPermissionException(NotLoginException e) {
        log.error("NotPermissionException", e);
        return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR, e.getMessage());
    }


    /**
     * 拦截自定义异常的
     *
     * @param e 异常
     * @return {@code BaseResponse<?> } 响应
     */
    @ExceptionHandler(BusinessException.class)
    public BaseResponse<?> handlerBusinessException(BusinessException e) {
        log.error("BusinessException",e);
        return ResultUtils.error(e.getCode(), e.getMessage());
    }

    /**
     * 兜底异常，抛出系统异常
     *
     * @param e 异常
     * @return {@code BaseResponse<?> } 响应
     */
    @ExceptionHandler(RuntimeException.class)
    public BaseResponse<?> handlerRuntimeException(RuntimeException e) {
        log.error("RuntimeException", e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR, e.getMessage());
    }
}
